package com.fintech.modules.opensearch;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.aliyun.opensearch.DocumentClient;
import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SearcherClient;
import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
import com.aliyun.opensearch.sdk.generated.search.Config;
import com.aliyun.opensearch.sdk.generated.search.Order;
import com.aliyun.opensearch.sdk.generated.search.SearchFormat;
import com.aliyun.opensearch.sdk.generated.search.SearchParams;
import com.aliyun.opensearch.sdk.generated.search.Sort;
import com.aliyun.opensearch.sdk.generated.search.SortField;
import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
import com.github.crab2died.ExcelUtils;
import com.pt.base.dto.MainDTO;
import com.pt.base.dto.MainOpenSearchDTO;

@Component("com.fintech.modules.opensearch.SearchBiz")
public class SearchBiz {
    private static Logger logger = LoggerFactory.getLogger(SearchBiz.class);
    
    public static boolean flag = true;
    public static boolean flag2 = true;
    public static List<String> excelList = new ArrayList<String>();
    
    public static String appName = "two";//应用名称
    public static String accesskey = "dd";//您的阿里云的Access Key ID
    public static String secret = "ss";//"阿里云 Access Key ID 对应的 Access Key Secret";
    public static String host = "http://opensearch-cn-beijing.aliyuncs.com";//"这里的host需要根据访问应用基本信息页中提供的的API入口来确定";
    
    //创建并构造OpenSearch对象
    public static OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
    //创建OpenSearchClient对象，并以OpenSearch对象作为构造参数
    public static OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
    
    public static void main(String[] args) {
    	System.out.println(250/100);
        /*SearchBiz biz = new SearchBiz();
        try {
            String result = biz.search("186",0,5);
            logger.info(result);
            
        } catch (OpenSearchException e) {
            e.printStackTrace();
        } catch (OpenSearchClientException e) {
            e.printStackTrace();
        }
*/
    }
    
    public void testParseAll(String path) throws Exception{
        int maxRows = 10000;
        if(StringUtils.isEmpty(path)){
            path = "F:\\验证类项目\\tmpExcel解析导入openSearch\\2\\";
        }

        File directory = new File(path);
        File[] files = directory.listFiles();
        flag = true;
        //开始信息
        this.outSomeString(path,"[");
        for(int i = 0;i < files.length;i ++){
            File tmp = files[i];
            if(tmp.getName().endsWith("xls") || tmp.getName().endsWith(".xlsx")){
                try {
                    System.out.println("====CG=读取Excel至String数组："+tmp.getAbsolutePath());
                    for(int k =0;k < 30;k ++){
                        System.out.println("kkkkkkkkkkkkk"+k);
                        List<List<String>> lists = ExcelUtils.getInstance().readExcel2List(tmp.getAbsolutePath(), 2,maxRows , k);
                        if(lists != null && lists.size() >0){
                            this.outTxt(directory.getCanonicalPath(),tmp.getAbsolutePath(),lists,(i+1+k)*maxRows);
                        }else{
                            break;
                        }
                    }
                }catch(IllegalArgumentException e1){
                    logger.error(e1.getMessage());
                    //e1.printStackTrace();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        //补充最后信息
        this.outSomeString(path,"]");
    }

    public StringBuffer parseAllExcelToStr(String path,StringBuffer data, int ecelCout) throws Exception{
        int maxRows = 10000;
        if(StringUtils.isEmpty(path)){
            path = "F:\\验证类项目\\tmpExcel解析导入openSearch\\2\\";
        }

        File directory = new File(path);
        File[] files = directory.listFiles();
        flag2 = true;
        //开始信息
        //logger.info("files.length===="+files.length);
        
        for(int i = 0;i < files.length;i ++){
        	File tmp = files[i];
        	//logger.info("file===="+tmp.getAbsolutePath());
            if(tmp.isDirectory()){
            	 this.parseAllExcelToStr(tmp.getAbsolutePath(),data,ecelCout);
            }
            
            if(tmp.getName().endsWith("xls") || tmp.getName().endsWith(".xlsx")){
            	
                try {
                	//logger.info("====CG=读取Excel至String数组："+tmp.getAbsolutePath());
                    for(int k =0;k < 30;k ++){
                    	logger.info("kkkkkkkkkkkkk{},excelList:{}",k,excelList.size());
                    	excelList.add("1");
                    	
                    	if(excelList.size() < ecelCout){
                    		logger.info("=====continue=========");
                    		continue;
                    	}
                    	
                    	if(excelList.size() > 10 + ecelCout){
                    		logger.info("=====break===1======");
                    		break;
                    	}
                    	
                    	logger.info("=====dealwith=========");
                        List<List<String>> lists = ExcelUtils.getInstance().readExcel2List(tmp.getAbsolutePath(), 2,maxRows , k);
                        if(lists != null && lists.size() >0){
                        	data = this.outStr(directory.getCanonicalPath(),tmp.getAbsolutePath(),lists,(i+1+k)*maxRows,data);
                        }else{
                            break;
                        }
                    }
                }catch(IllegalArgumentException e1){
                    logger.error("==tmp.getAbsolutePath():"+tmp.getAbsolutePath(),e1.getMessage());
                }catch (Exception e) {
                	System.out.println("==Exception ==tmp.getAbsolutePath():"+tmp.getAbsolutePath());
                	e.printStackTrace();
                }
            }
            
            if(excelList.size() > 10 + ecelCout){
        		logger.info("=====break==2=======");
        		break;
        	}
        }
        
        //补充最后信息
        //data.append("]");
        
        return data;
    }
    
    /**
     * [{"fields":
     * {"id":"0"
     * ,"type_id":71
     * ,"cat_id":97
     * ,"title":"云搜索 "
     * ,"body":"广大中小企业都有各种结构化的数据需要进行检索，目前一般采用数据库本身提供的搜索功能或者利用open source的搜索软件搭建，这样的做法不但会消耗网站本身的资源，性能也会很容易成为问题，而且相关性通常也不够好。我们的产品的目的是要利用阿里云先进的云计算和搜索技术向广大中小企业提供低成本，高质量，高性能，可定制的数据搜索解决方案。本项目和云搜索的通用解决方案目标略有不同，主要区别为本项目主要针对用户的结构化数据进行搜索，云搜索的通用解决方案则主要是针对网页型数据位处理对象。"
     * ,"url":"新蛋商城"
     * ,"author":"作者"
     * ,"thumbnail":"京东商城"
     * ,"source":"来源"
     * ,"create_timestamp":66
     * ,"update_timestamp":63
     * ,"hit_num":29
     * ,"focus_count":58
     * ,"grade":81
     * ,"comment_count":10
     * ,"tag":"专业词典"}
     * ,"cmd":"ADD"}]
     * @param data
     * @throws OpenSearchException
     * @throws OpenSearchClientException
     */
    public void uploadBatchData(String data) throws OpenSearchException, OpenSearchClientException{
        DocumentClient doc = new DocumentClient(serviceClient);
        String tableName = "main";//"要上传数据的表名";
        OpenSearchResult osr = doc.push(data, appName, tableName);
        logger.info("uploadBatchData result:{}",osr.getResult());
    }
    
    public String search(String param,int start,int hists) throws OpenSearchException, OpenSearchClientException{
        String result ="";
        
        //创建SearcherClient对象，并以OpenSearchClient对象作为构造参数
        SearcherClient searcherClient = new SearcherClient(serviceClient);
        //定义Config对象，用于设定config子句参数，指定应用名，分页，数据返回格式等等
        Config config = new Config(Lists.newArrayList(appName));
        config.setStart(start);
        config.setHits(hists);
        //设置返回格式为fulljson格式
        config.setSearchFormat(SearchFormat.JSON);
        // 创建参数对象
        SearchParams searchParams = new SearchParams(config);
        // 指定搜索的关键词，这里要指定在哪个索引上搜索，如果不指定的话默认在使用“default”索引（索引字段名称是您在您的数据结构中的“索引字段列表”中对应字段。），若需多个索引组合查询，需要在setQuery处合并，否则若设置多个setQuery子句，则后面的子句会替换前面子句
        //searchParams.setQuery("name:'搜索'");
        //searchParams.setQuery("default:'"+param+"'");
        searchParams.setQuery(param);
        //设置查询过滤条件
        //searchParams.setFilter("id>0");
        //创建sort对象，并设置二维排序
        Sort sort = new Sort();
        //设置id字段降序
        sort.addToSortFields(new SortField("id", Order.DECREASE));
        //若id相同则以RANK相关性算分升序
        sort.addToSortFields(new SortField("RANK", Order.INCREASE));
        //添加Sort对象参数
        //searchParams.setSort(sort);
        //执行查询语句返回数据对象
        SearchResult searchResult = searcherClient.execute(searchParams);
        //以字符串返回查询数据
        result = searchResult.getResult();
        
        return result;
    }
    
    
    
    private StringBuffer outStr(String txtPath,String excelPath,List<List<String>> lists,int i,StringBuffer sb) {
        
        int k =0;
        for (List<String> list : lists) {
            k=k+1;
            MainDTO tableDTO = new MainDTO();
            tableDTO.setId(String.valueOf(i+k));
            tableDTO.setBody(list.toString());
            tableDTO.setCreate_timestamp(String.valueOf(new Date().getTime()));
            tableDTO.setAuthor(excelPath);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            tableDTO.setTag(sdf.format(new Date()));


            MainOpenSearchDTO dto = new MainOpenSearchDTO();
            dto.setCmd("ADD");
            dto.setFields(tableDTO);
            //fw.write(","+JSON.toJSONString(dto) + "\r\n");
            if(flag2){
                sb.append(JSON.toJSONString(dto));
            }else{
                sb.append(",").append(JSON.toJSONString(dto));
            }
            flag2 = false;
        }
        
        return sb;
        
    }

    private void outSomeString(String path, String someStr) throws Exception {
        FileWriter fw = null;
        try {
            fw = new FileWriter(path + "\\result2.txt",true);
            fw.write(someStr);
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    private void outTxt(String txtPath,String excelPath,List<List<String>> lists,int i){
        FileWriter fw = null;
        try {
            //加true表示在写在文件的结尾而不是开头
            fw = new FileWriter(txtPath + "\\result2.txt",true);
            //fw.write("+++++++++"+excelPath+"+++++++++\r\n");
            int k =0;
            for (List<String> list : lists) {
                k=k+1;
                /*MyContentDTO tableDTO = new MyContentDTO();
                tableDTO.setContent(list.toString());
                tableDTO.setUuid(String.valueOf(new Date().getTime()));
                tableDTO.setExcelPath(excelPath);*/
                MainDTO tableDTO = new MainDTO();
                tableDTO.setId(String.valueOf(i+k));
                tableDTO.setBody(list.toString());
                tableDTO.setCreate_timestamp(String.valueOf(new Date().getTime()));
                tableDTO.setAuthor(excelPath);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                tableDTO.setTag(sdf.format(new Date()));


                MainOpenSearchDTO dto = new MainOpenSearchDTO();
                dto.setCmd("ADD");
                dto.setFields(tableDTO);
                //fw.write(","+JSON.toJSONString(dto) + "\r\n");
                if(flag){
                    fw.write(JSON.toJSONString(dto));
                }else{
                    fw.write(","+JSON.toJSONString(dto));
                }
                flag = false;
                //fw.write(list + "\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    

}
